package com.onlyxiahui.framework.json.validator.impl;

import java.util.Collection;
import java.util.List;
import java.util.Set;

import com.alibaba.fastjson.JSONObject;
import com.onlyxiahui.framework.json.validator.Validator;
import com.onlyxiahui.framework.json.validator.ValidatorError;
import com.onlyxiahui.framework.json.validator.ValidatorResult;

/**
 * 
 * Date 2018-12-26 15:15:47<br>
 * Description
 * 
 * @author XiaHui<br>
 * @since 1.0.0
 */
public class JsonArrayValidator implements Validator<JSONObject, Object> {

	String message = "只能是数组！";
	String code = ValidatorError.type_error;

	@Override
	public String getKey() {
		return "array";
	}

	@Override
	public ValidatorResult valid(JSONObject data, Object attribute, Object judge, String code, String message) {
		ValidatorResult vr = null;
		boolean mark = true;
		if (null != attribute) {
			boolean isCollection = attribute instanceof Collection;
			boolean isList = attribute instanceof List;
			boolean isSet = attribute instanceof Set;
			boolean isArray = attribute.getClass().isArray();
			mark = isCollection || isList || isSet || isArray;
		}

		if (!mark) {

			vr = new ValidatorResult();
			vr.setErrorCode(code == null ? this.code : code);
			vr.setMessage(message == null ? this.message : message);
		}
		return vr;
	}
}
